package calvin.study.springboot.dao;

import java.util.Date;
import java.util.Map;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class ProductDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	public int insert(String productName) {
		Map<String, Object> map = jdbcTemplate.queryForMap("SELECT MAX(product_id) as product_id from  `t_product`");
		
		int productId = Integer.parseInt(map.get("product_id").toString()) + 1;
		System.out.println(productId);

		String sql = "INSERT INTO `t_product`(product_id,product_name,product_number,create_time) VALUES(?,?,?,?)";

		return jdbcTemplate.update(sql, productId, productName, new Random().nextInt(100),new Date());
	}
	
	public int update(int productId,int number) {
		return jdbcTemplate.update("UPDATE t_product SET product_number = ? WHERE product_id = ?",number,productId);
	}
	
}
